﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;

/// <summary>
/// 品类维护
/// Update By:Allen Jiang
/// Update Time:2008-12-22
/// Description:
///     修改品类维护在一个页面。
/// </summary>
public partial class masterdata_Category_Type_List : PageBase
{
    CategoryManager manager = new CategoryManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CategoryBind();
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    ///
   
    public void CategoryBind()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder("this.CategoryName like '%%' ");
 
        GridPager1.DataSource = manager.GetCategoryTable(sb.ToString(), MyStateManager.DomainID);
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
        GridPager1.Visible = GridView1.Rows.Count > 0;

        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
        
    }
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;
        TextBox ctl = DetailsView1.FindControl("txtCode") as TextBox;

        if (DetailsView1.DataItem == null)
        {
            chk.Checked = true;
        }
        else
        {
            ctl.Enabled = false;
        }
    }

    /// <summary>
    /// 保存数据方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            CATEGORIES category = null;
            CategoryManager manager = new CategoryManager();
            TextBox ctlcode = DetailsView1.FindControl("txtCode") as TextBox;
            TextBox ctl = DetailsView1.FindControl("txtName") as TextBox;

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                category = new CATEGORIES();
                category.CREATETIME = long.Parse(Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeString(DateTime.Now));
                //判断是否已经存在此代码
                if (manager.LoadDomianID(ctlcode.Text, MyStateManager.DomainID) != null)
                {
                    ShowMessage("已存在此代码");
                    return;
                }
                //判断是否已经存在此名称
                if (manager.LoadName(ctl.Text,MyStateManager.DomainID)!=null)
                {
                    ShowMessage("已存在此名称");
                    return;
                }

                category.CategoryCode = ctlcode.Text;
            }
            else
            {
                CATEGORIES c=manager.LoadName(ctl.Text, MyStateManager.DomainID);
                //判断是否已经存在此名称
                if (c!= null)
                {
                    //不是同一ID的就重复
                    if (!c.CategoryCode.Equals(ctlcode.Text))
                    {
                        ShowMessage("已存在此名称");
                        return;
                    }
                }
                category = manager.Load(int.Parse(GridView1.SelectedDataKey.Value.ToString()));
            }
            category.CategoryName = ctl.Text;
            
            ctl.Dispose();
            ctl = null;

            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            category.ACTIVE = cbxValid.Checked;
            category.DOMAINID = MyStateManager.DomainID;
            category.LAST_UPDATER = MyStateManager.UserID.ToString();
           

            string LogContent = "";

            if (category.State==DataRowState.Added)
            {
                LogContent += "增加新的品类，" + ctlcode.Text + "的信息是：";
            }
            else
            {
                LogContent += category.CategoryCode + "信息发生变动，变动后信息是：";
            }
            LogContent += "品类代码：" + category.CategoryCode + "，";
            LogContent += "品类名称：" + category.CategoryName + "，";
            LogContent += "有效：" + category.ACTIVE + "，";
            LogContent += "操作人：" + category.LAST_UPDATER;

            manager.Save(category);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
             Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
             this.MyStateManager.UserID, LogContent);
            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                CategoryBind();
            }
            else
            {
                Response.Redirect(Request.RawUrl.ToString());
            }
          
           
        }
        catch (Exception ex)
        {
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                    Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                    this.MyStateManager.UserID, "品类维护 - 异常:" + ex.Message);
        }
    }

   
    protected String Summit()
    {
        return "$get('" + btnSave.ClientID + "').click();";
    }

  
    /// <summary>
    /// 新增方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
     
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        CategoryBind();
    }
   
}
